/*
 * INSANE - Interactive Structural Analysis Environment
 *
 * Copyright (C) 2003-2006
 * Universidade Federal de Minas Gerais
 * Escola de Engenharia
 * Departamento de Engenharia de Estruturas
 *
 * Author's email :     insane@dees.ufmg.br
 * Author's Website :   http://www.dees.ufmg.br/insane
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */
 
package br.ufmg.dees.insane.draw;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.Vector;

/**
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @since	August 2004
 */
public abstract class GridSpringDraw extends Draw {
	
//*****************************************************************************
	
	public static class X extends GridSpringDraw {
		
	//*****************************************************************************
		
		public X() {
			this( new Point(0, 0) );
		}
		
	//*****************************************************************************
		
		public X(Point loc) {
			this( loc, 0 );
		}
		
	//*****************************************************************************
		
		public X(Point loc, double theta) {
			this( loc, theta, null );
		}
		
	//*****************************************************************************
		
		public X(Point loc, double theta, Object represents) {
			this.setLocation(loc);
			this.setAngle(theta);
			this.setRepresents(represents);
			this.setSize(new Dimension(36, 24));
		}
		
	//*****************************************************************************
		
		public Vector<Object> getShapes() {
			
			Vector<Object> shapes = new Vector<Object>();
			
            int x = this.getLocation().x;
            int y = this.getLocation().y;
            double scale = this.getScale();
            
            double H = 20 * scale;
			double dist = 5 * scale;
			
			shapes.clear();
			
            shapes.add(new Line2D.Double(x-H/2, y-H/2, x+H/2, y-H/2));
			shapes.add(new Line2D.Double(x+H/2, y-H/2, x+H/2, y+H/2));
			shapes.add(new Line2D.Double(x+H/2, y+H/2, x-H/2, y+H/2));
			shapes.add(new Line2D.Double(x-H/2, y+H/2, x-H/2, y-H/2));
			
			shapes.add(new Line2D.Double(x-H/2, y+H/2-dist, x+H/2, y+H/2-dist));
			shapes.add(new Line2D.Double(x-H/2, y-H/2+dist, x+H/2, y-H/2+dist));
			
			for (int i=0; i<4; i++) {
				shapes.add(new Line2D.Double(x-H/2+i*dist, y-H/2, x-H/2+dist+i*dist, y-H/2+dist));
				shapes.add(new Line2D.Double(x-H/2+i*dist, y-H/2+dist, x-H/2+dist+i*dist, y-H/2));
				shapes.add(new Line2D.Double(x-H/2+i*dist, y+H/2, x-H/2+dist+i*dist, y+H/2-dist));
				shapes.add(new Line2D.Double(x-H/2+i*dist, y+H/2-dist, x-H/2+dist+i*dist, y+H/2));
			}
			
			return shapes;
			
		}
		
	//*****************************************************************************
		
		/**
		 * Returns the draw bounds in device units.
		 *
		 * @return the draw bounds
		 */
		 public Rectangle getBounds() {
			 
			 int x = this.getLocation().x;
			 int y = this.getLocation().y;
			 int width = this.getSize().width;
			 int height = this.getSize().height;
			 double scale = this.getScale();
			 
			 int W = (int)(width * scale);
			 int H = (int)(width * scale);
			 int h = (int)(height/6 * scale);
			 return new Rectangle((int)(x - W/2), y, W, H - h);
		 }
		 
	}
	
//*****************************************************************************
	
	public static class Y extends GridSpringDraw {
		
	//*****************************************************************************
		
		public Y() {
			this( new Point(0, 0) );
		}
		
	//*****************************************************************************
		
		public Y(Point loc) {
			this( loc, 0 );
		}
		
	//*****************************************************************************
		
		public Y(Point loc, double theta) {
			this( loc, theta, null );
		}
		
	//*****************************************************************************
		
		public Y(Point loc, double theta, Object represents) {
			this.setLocation(loc);
			this.setAngle(theta);
			this.setRepresents(represents);
			this.setSize(new Dimension(24, 36));
		}
		
	//*****************************************************************************
		
		public Vector<Object> getShapes() {
			
			Vector<Object> shapes = new Vector<Object>();
			
            int x = this.getLocation().x;
            int y = this.getLocation().y;
            double scale = this.getScale();
            
			double H = 20 * scale;
			
			shapes.clear();
			
            shapes.add(new Ellipse2D.Double(x-H/2, y-H/2, H, H));
			
			shapes.add(new Line2D.Double(x-H/(2*Math.sqrt(2)), y-H/(2*Math.sqrt(2)), x+H/(2*Math.sqrt(2)), y+H/(2*Math.sqrt(2))));
			shapes.add(new Line2D.Double(x-H/(2*Math.sqrt(2)), y+H/(2*Math.sqrt(2)), x+H/(2*Math.sqrt(2)), y-H/(2*Math.sqrt(2))));
			
			shapes.add(new Line2D.Double(x-H/2, y, x, y+H/2));
			shapes.add(new Line2D.Double(x, y+H/2, x+H/2, y));
			shapes.add(new Line2D.Double(x+H/2, y, x, y-H/2));
			shapes.add(new Line2D.Double(x, y-H/2, x-H/2, y));
			
			return shapes;
		}
		
	//*****************************************************************************
		
		/**
		 * Returns the draw bounds in device units.
		 *
		 * @return the draw bounds
		 */
		 public Rectangle getBounds() {
			 
			 int x = this.getLocation().x;
			 int y = this.getLocation().y;
			 int width = this.getSize().width;
			 int height = this.getSize().height;
			 double scale = this.getScale();
			 
			 int W = (int)(width * scale);
			 int H = (int)(width * scale);
			 int h = (int)(height/6 * scale);
			 return new Rectangle((int)(x - W/2), y, W, H - h);
		 }
		 
	}
	
//*****************************************************************************
//*****************************************************************************
	
	public static class XY extends GridSpringDraw {
	
	//*****************************************************************************
		
		public XY() {
			this(new Point(0, 0), 0);
		}
		
	//*****************************************************************************
		
		public XY(Point loc) {
			this(loc, 0);
		}
		
	//*****************************************************************************
		
		public XY(Point loc, double theta) {
			this(loc, theta, null);
		}
		
	//*****************************************************************************
		
		public XY(Point loc, double theta, Object represents) {
			this.setLocation(loc);
			this.setAngle(theta);
			this.setRepresents(represents);
			this.setSize(new Dimension(20, 20));
		}
		
	//*****************************************************************************
		
		public Vector<Object> getShapes() {
			
			Vector<Object> shapes = new Vector<Object>();
			
            int x = this.getLocation().x;
            int y = this.getLocation().y;
            double scale = this.getScale();
            
            double H = 20 * scale;
			double dist = 5 * scale;
			
			shapes.clear();
			
            shapes.add(new Line2D.Double(x-H/2, y-H/2, x+H/2, y-H/2));
			shapes.add(new Line2D.Double(x+H/2, y-H/2, x+H/2, y+H/2));
			shapes.add(new Line2D.Double(x+H/2, y+H/2, x-H/2, y+H/2));
			shapes.add(new Line2D.Double(x-H/2, y+H/2, x-H/2, y-H/2));
			
			//X
			shapes.add(new Line2D.Double(x-H/2, y+H/2-dist, x+H/2, y+H/2-dist));
			shapes.add(new Line2D.Double(x-H/2, y-H/2+dist, x+H/2, y-H/2+dist));
			
			for (int i=0; i<4; i++) {
				shapes.add(new Line2D.Double(x-H/2+i*dist, y-H/2, x-H/2+dist+i*dist, y-H/2+dist));
				shapes.add(new Line2D.Double(x-H/2+i*dist, y-H/2+dist, x-H/2+dist+i*dist, y-H/2));
				shapes.add(new Line2D.Double(x-H/2+i*dist, y+H/2, x-H/2+dist+i*dist, y+H/2-dist));
				shapes.add(new Line2D.Double(x-H/2+i*dist, y+H/2-dist, x-H/2+dist+i*dist, y+H/2));
			}
			
			//Y
			dist = dist / 2;
			H = H /2;
			
            shapes.add(new Ellipse2D.Double(x-H/2, y-H/2, H, H));
			shapes.add(new Line2D.Double(x-H/(2*Math.sqrt(2)), y-H/(2*Math.sqrt(2)), x+H/(2*Math.sqrt(2)), y+H/(2*Math.sqrt(2))));
			shapes.add(new Line2D.Double(x-H/(2*Math.sqrt(2)), y+H/(2*Math.sqrt(2)), x+H/(2*Math.sqrt(2)), y-H/(2*Math.sqrt(2))));
			
			shapes.add(new Line2D.Double(x-H/2, y, x, y+H/2));
			shapes.add(new Line2D.Double(x, y+H/2, x+H/2, y));
			shapes.add(new Line2D.Double(x+H/2, y, x, y-H/2));
			shapes.add(new Line2D.Double(x, y-H/2, x-H/2, y));
			
			return shapes;
		}
		
	//*****************************************************************************
		
		/**
		 * Returns the draw bounds in device units.
		 *
		 * @return the draw bounds
		 */
		 public Rectangle getBounds() {
			 
			 int x = this.getLocation().x;
			 int y = this.getLocation().y;
			 int width = this.getSize().width;
			 int height = this.getSize().height;
			 double scale = this.getScale();
			 
			 int W = (int)(width * scale);
			 int H = (int)(width * scale);
			 int h = (int)(height/6 * scale);
			 return new Rectangle((int)(x - W/2), y, W, H - h);
		 }
		 
	}
	
//*****************************************************************************
//*****************************************************************************
	
	public static class XZ extends GridSpringDraw {
	
	//*****************************************************************************
		
		public XZ() {
			this(new Point(0, 0), 0);
		}
		
	//*****************************************************************************
		
		public XZ(Point loc) {
			this(loc, 0);
		}
		
	//*****************************************************************************
		
		public XZ(Point loc, double theta) {
			this(loc, theta, null);
		}
		
	//*****************************************************************************
		
		public XZ(Point loc, double theta, Object represents) {
			this.setLocation(loc);
			this.setAngle(theta);
			this.setRepresents(represents);
			this.setSize(new Dimension(20, 20));
		}
		
	//*****************************************************************************
		
		public Vector<Object> getShapes() {
			
			Vector<Object> shapes = new Vector<Object>();
			
            int x = this.getLocation().x;
            int y = this.getLocation().y;
            double scale = this.getScale();
            
            double H = 20 * scale;
			double dist = 5 * scale;
			
			shapes.clear();
			
            shapes.add(new Line2D.Double(x-H/2, y-H/2, x+H/2, y-H/2));
			shapes.add(new Line2D.Double(x+H/2, y-H/2, x+H/2, y+H/2));
			shapes.add(new Line2D.Double(x+H/2, y+H/2, x-H/2, y+H/2));
			shapes.add(new Line2D.Double(x-H/2, y+H/2, x-H/2, y-H/2));
			
			//X
			shapes.add(new Line2D.Double(x-H/2, y+H/2-dist, x+H/2, y+H/2-dist));
			shapes.add(new Line2D.Double(x-H/2, y-H/2+dist, x+H/2, y-H/2+dist));
			
			for (int i=0; i<4; i++) {
				shapes.add(new Line2D.Double(x-H/2+i*dist, y-H/2, x-H/2+dist+i*dist, y-H/2+dist));
				shapes.add(new Line2D.Double(x-H/2+i*dist, y-H/2+dist, x-H/2+dist+i*dist, y-H/2));
				shapes.add(new Line2D.Double(x-H/2+i*dist, y+H/2, x-H/2+dist+i*dist, y+H/2-dist));
				shapes.add(new Line2D.Double(x-H/2+i*dist, y+H/2-dist, x-H/2+dist+i*dist, y+H/2));
			}
			
			//Z
			shapes.add(new Line2D.Double(x-H/2+dist, y+H/2, x-H/2+dist, y-H/2));
			shapes.add(new Line2D.Double(x+H/2-dist, y+H/2, x+H/2-dist, y-H/2));
			
			for (int i=0; i<4; i++) {
				shapes.add(new Line2D.Double(x-H/2, y-H/2+i*dist, x-H/2+dist, y-H/2+dist+i*dist));
				shapes.add(new Line2D.Double(x-H/2, y+H/2-i*dist, x-H/2+dist, y+H/2-dist-i*dist));
				shapes.add(new Line2D.Double(x+H/2, y-H/2+i*dist, x+H/2-dist, y-H/2+dist+i*dist));
				shapes.add(new Line2D.Double(x+H/2, y+H/2-i*dist, x+H/2-dist, y+H/2-dist-i*dist));
			}
			
			return shapes;
		}
		
	//*****************************************************************************
		
		/**
		 * Returns the draw bounds in device units.
		 *
		 * @return the draw bounds
		 */
		 public Rectangle getBounds() {
			 
			 int x = this.getLocation().x;
			 int y = this.getLocation().y;
			 int width = this.getSize().width;
			 int height = this.getSize().height;
			 double scale = this.getScale();
			 
			 int W = (int)(width * scale);
			 int H = (int)(width * scale);
			 int h = (int)(height/6 * scale);
			 return new Rectangle((int)(x - W/2), y, W, H - h);
		 }
		 
	}
	
//*****************************************************************************
//*****************************************************************************
	
	public static class XYZ extends GridSpringDraw {
	
	//*****************************************************************************
		
		public XYZ() {
			this(new Point(0, 0), 0);
		}
		
	//*****************************************************************************
		
		public XYZ(Point loc) {
			this(loc, 0);
		}
		
	//*****************************************************************************
		
		public XYZ(Point loc, double theta) {
			this(loc, theta, null);
		}
		
	//*****************************************************************************
		
		public XYZ(Point loc, double theta, Object represents) {
			this.setLocation(loc);
			this.setAngle(theta);
			this.setRepresents(represents);
			this.setSize(new Dimension(20, 20));
		}
		
	//*****************************************************************************
		
		public Vector<Object> getShapes() {
			
			Vector<Object> shapes = new Vector<Object>();
			
            int x = this.getLocation().x;
            int y = this.getLocation().y;
            double scale = this.getScale();
            
            double H = 20 * scale;
			double dist = 5 * scale;
			
			shapes.clear();
			
            shapes.add(new Line2D.Double(x-H/2, y-H/2, x+H/2, y-H/2));
			shapes.add(new Line2D.Double(x+H/2, y-H/2, x+H/2, y+H/2));
			shapes.add(new Line2D.Double(x+H/2, y+H/2, x-H/2, y+H/2));
			shapes.add(new Line2D.Double(x-H/2, y+H/2, x-H/2, y-H/2));
			
			//X
			shapes.add(new Line2D.Double(x-H/2, y+H/2-dist, x+H/2, y+H/2-dist));
			shapes.add(new Line2D.Double(x-H/2, y-H/2+dist, x+H/2, y-H/2+dist));
			
			for (int i=0; i<4; i++) {
				shapes.add(new Line2D.Double(x-H/2+i*dist, y-H/2, x-H/2+dist+i*dist, y-H/2+dist));
				shapes.add(new Line2D.Double(x-H/2+i*dist, y-H/2+dist, x-H/2+dist+i*dist, y-H/2));
				shapes.add(new Line2D.Double(x-H/2+i*dist, y+H/2, x-H/2+dist+i*dist, y+H/2-dist));
				shapes.add(new Line2D.Double(x-H/2+i*dist, y+H/2-dist, x-H/2+dist+i*dist, y+H/2));
			}
			
			//Z
			shapes.add(new Line2D.Double(x-H/2+dist, y+H/2, x-H/2+dist, y-H/2));
			shapes.add(new Line2D.Double(x+H/2-dist, y+H/2, x+H/2-dist, y-H/2));
			
			for (int i=0; i<4; i++) {
				shapes.add(new Line2D.Double(x-H/2, y-H/2+i*dist, x-H/2+dist, y-H/2+dist+i*dist));
				shapes.add(new Line2D.Double(x-H/2, y+H/2-i*dist, x-H/2+dist, y+H/2-dist-i*dist));
				shapes.add(new Line2D.Double(x+H/2, y-H/2+i*dist, x+H/2-dist, y-H/2+dist+i*dist));
				shapes.add(new Line2D.Double(x+H/2, y+H/2-i*dist, x+H/2-dist, y+H/2-dist-i*dist));
			}
			
			//Y
			dist = dist / 2;
			H = H / 2;
			
			shapes.add(new Ellipse2D.Double(x-H/2, y-H/2, H, H));
			
			shapes.add(new Line2D.Double(x-H/(2*Math.sqrt(2)), y-H/(2*Math.sqrt(2)), x+H/(2*Math.sqrt(2)), y+H/(2*Math.sqrt(2))));
			shapes.add(new Line2D.Double(x-H/(2*Math.sqrt(2)), y+H/(2*Math.sqrt(2)), x+H/(2*Math.sqrt(2)), y-H/(2*Math.sqrt(2))));
			
			shapes.add(new Line2D.Double(x-H/2, y, x, y+H/2));
			shapes.add(new Line2D.Double(x, y+H/2, x+H/2, y));
			shapes.add(new Line2D.Double(x+H/2, y, x, y-H/2));
			shapes.add(new Line2D.Double(x, y-H/2, x-H/2, y));
			
			return shapes;
		}
		
	//*****************************************************************************
		
		/**
		 * Returns the draw bounds in device units.
		 *
		 * @return the draw bounds
		 */
		 public Rectangle getBounds() {
			 
			 int x = this.getLocation().x;
			 int y = this.getLocation().y;
			 int width = this.getSize().width;
			 int height = this.getSize().height;
			 double scale = this.getScale();
			 
			 int W = (int)(width * scale);
			 int H = (int)(width * scale);
			 int h = (int)(height/6 * scale);
			 return new Rectangle((int)(x - W/2), y, W, H - h);
		 }
		 
	}
	
//*****************************************************************************
//*****************************************************************************
	
	//This class exists just to avoid combinig supports ans spring draws
	//It must be identical to Y class, producing a draw whose size is exactly
	//half of the size of Y
	public static class halfY extends GridSpringDraw {
		
	//*****************************************************************************
		
		public halfY() {
			this( new Point(0, 0) );
		}
		
	//*****************************************************************************
		
		public halfY(Point loc) {
			this( loc, 0 );
		}
		
	//*****************************************************************************
		
		public halfY(Point loc, double theta) {
			this( loc, theta, null );
		}
		
	//*****************************************************************************
		
		public halfY(Point loc, double theta, Object represents) {
			this.setLocation(loc);
			this.setAngle(theta);
			this.setRepresents(represents);
			this.setSize(new Dimension(24, 36));
		}
		
	//*****************************************************************************
		
		public Vector<Object> getShapes() {
			
			Vector<Object> shapes = new Vector<Object>();
			
            int x = this.getLocation().x;
            int y = this.getLocation().y;
            double scale = this.getScale();
            
			double H = 10 * scale;
			
			shapes.clear();
			
            shapes.add(new Ellipse2D.Double(x-H/2, y-H/2, H, H));
			
			shapes.add(new Line2D.Double(x-H/(2*Math.sqrt(2)), y-H/(2*Math.sqrt(2)), x+H/(2*Math.sqrt(2)), y+H/(2*Math.sqrt(2))));
			shapes.add(new Line2D.Double(x-H/(2*Math.sqrt(2)), y+H/(2*Math.sqrt(2)), x+H/(2*Math.sqrt(2)), y-H/(2*Math.sqrt(2))));
			
			shapes.add(new Line2D.Double(x-H/2, y, x, y+H/2));
			shapes.add(new Line2D.Double(x, y+H/2, x+H/2, y));
			shapes.add(new Line2D.Double(x+H/2, y, x, y-H/2));
			shapes.add(new Line2D.Double(x, y-H/2, x-H/2, y));
			
			return shapes;
		}
		
	//*****************************************************************************
		
		/**
		 * Returns the draw bounds in device units.
		 *
		 * @return the draw bounds
		 */
		 public Rectangle getBounds() {
			 
			 int x = this.getLocation().x;
			 int y = this.getLocation().y;
			 int width = this.getSize().width;
			 int height = this.getSize().height;
			 double scale = this.getScale();
			 
			 int W = (int)(width * scale);
			 int H = (int)(width * scale);
			 int h = (int)(height/6 * scale);
			 return new Rectangle((int)(x - W/2), y, W, H - h);
		 }
		 
	}
	
//*****************************************************************************
//*****************************************************************************
}
